from django.db import models
from datetime import datetime
from django.contrib.auth.models import AbstractUser, Group


# 自定义一个部门库
# 系统自带的Group支持权限，作为用户角色字段
class Department(models.Model):
    name = models.CharField(max_length=20, verbose_name='部门名称')
    pid = models.IntegerField(default=0, verbose_name='上级节点名称')
    leader = models.CharField(null=True, max_length=20, verbose_name='部门负责人')

    class Meta:
        verbose_name = '用户模块-部门'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 职位信息
class Position(models.Model):
    role_choicse = [
        (0, '系统管理'),
        (1, '企业股东'),
        (2, '高层领导'),
        (3, '主管经理'),
        (4, '普通职员'),
        (5, '实习生'),
    ]
    name = models.CharField(max_length=10, verbose_name='职位名称')
    # 等级用于区分普通职位和高层职位以区分不一样的权限
    role = models.IntegerField(choices=role_choicse, default=4, verbose_name='职位等级')

    class Meta:
        verbose_name = '用户模块-职位'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 定义新用户模型
class UserProfile(AbstractUser):
    # 删除字段
    first_name = None
    last_name = None
    # user_permissions = None   #不要针对用户设置权限
    # 新增字段
    # 用户职位
    position = models.ForeignKey(Position, on_delete=models.CASCADE, verbose_name='用户职位', null=True)
    # 用户角色（权限设置）
    role = models.ForeignKey(Group, on_delete=models.CASCADE, verbose_name='用户角色', null=True)
    # 用户部门
    department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='用户部门', null=True)
    # 用户头像
    head_pic = models.TextField(null=True, verbose_name='用户头像')
    mobile = models.CharField(max_length=11, verbose_name='手机号码', blank=True)
    force_change_passwd = models.BooleanField(default=True, verbose_name='强制修改密码')
    code = models.CharField(max_length=6, verbose_name='验证码', null=True)
    code_expire = models.DateTimeField(verbose_name='验证码过期时间', null=True)
    direct_leader = models.CharField(max_length=20, verbose_name='直属领导', null=True)
    # 微信扫描登录绑定unionid
    union_id = models.CharField(max_length=64, verbose_name='微信扫描登录UnionID', null=True)
    # 企业微信绑定的Openid
    open_id = models.CharField(max_length=64, verbose_name='企业微信扫描登录ID', null=True)
    # 钉钉绑定的ID
    dingtalk_id = models.CharField(max_length=64, verbose_name='钉钉扫描登录ID', null=True)

    class Meta:
        verbose_name = '用户模块-用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


# 日志记录模块
class Log(models.Model):
    username = models.CharField(max_length=20, verbose_name='账号')
    content = models.CharField(max_length=500, verbose_name='操作内容')
    modify_time = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')

    class Meta:
        verbose_name = '用户模块-日志'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username + '.' + self.content


# 站内信
class Msg(models.Model):
    receiver = models.ForeignKey(to=UserProfile, on_delete=models.CASCADE, verbose_name='接受者', related_name='receiver')
    sender = models.ForeignKey(to=UserProfile, on_delete=models.CASCADE, verbose_name='发送者', related_name='sender')
    subject = models.CharField(max_length=200, verbose_name='站内信主题')
    contents = models.CharField(max_length=2000, verbose_name='站内信内容', default='站内信')
    read_status = models.BooleanField(default=False, verbose_name='阅读状态')
    send_time = models.DateTimeField(auto_now_add=True, verbose_name='发送时间')
    read_time = models.DateTimeField(null=True, verbose_name='阅读时间')

    class Meta:
        verbose_name = '用户模块-站内信'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.subject


# 给Group添加新字段
# Group.add_to_class('parent_id', models.IntegerField(verbose_name='父类ID', default=0))
# Group.add_to_class('leader', models.CharField(max_length=20, verbose_name='部门主管', null=True))

# 菜单
class Menu(models.Model):
    name = models.CharField(max_length=20, verbose_name="菜单名")
    icon = models.CharField(max_length=200, verbose_name="icon")
    url = models.CharField(max_length=200, verbose_name="链接", null=True)
    pid = models.IntegerField(verbose_name="父级", default=0)
    order_num = models.IntegerField(default=0, verbose_name="排序号")
    show = models.BooleanField(default=True, verbose_name="是否展现")

    class Meta:
        verbose_name = '用户模块-菜单'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 权限（view根据权限处理数据，前端根据权限展现菜单）
class CustomPerms(models.Model):
    type_choices = [
        (1, "查看"),
        (2, "新增/修改"),
        (3, "删除"),
    ]
    to_menu = models.ForeignKey(Menu, on_delete=models.CASCADE, verbose_name="关联菜单")
    type = models.IntegerField(choices=type_choices, verbose_name="权限类型")
    perms = models.ManyToManyField(UserProfile, verbose_name="用户权限")  # 用户权限
    perms_group = models.ManyToManyField(Group, verbose_name="组权限")  # 组权限

    class Meta:
        verbose_name = '用户模块-权限'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.type
